package com.wuliupai.delivery.wuliupaidelivery.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wuliupai.delivery.wuliupaidelivery.R;
import com.wuliupai.delivery.wuliupaidelivery.bean2.task.TaskBean;
import com.wuliupai.delivery.wuliupaidelivery.constants.Value;
import com.wuliupai.delivery.wuliupaidelivery.utils.DateUtils_;
import com.wuliupai.delivery.wuliupaidelivery.utils.MoneyUtil_;
import com.wuliupai.delivery.wuliupaidelivery.utils.NumberUtils;
import com.wuliupai.delivery.wuliupaidelivery.utils.StringUtils;

import java.util.List;

/**
 * Descriptions：任务列表
 * <p>
 * Author：ChenME
 * Date：5/3/2017
 * Email：ibelieve1210@163.com
 */
public class TaskListAdapter extends RecyclerView.Adapter<TaskListAdapter.MyOrderHolder> {

    private MoneyUtil_ moneyUtil;
    private DateUtils_ dateUtils;

    private Context context;
    private List<TaskBean> taskList;
    private OnItemClickListener onItemClickListener;

    public static final int Page_Current = 1,
            Page_Record = 2;
    private int whichPage; // 哪一个页面

    /**
     * @param context
     * @param taskList
     */
    public TaskListAdapter(Context context, List<TaskBean> taskList, int whichPage) {
        moneyUtil = MoneyUtil_.getInstance_(context);
        dateUtils = DateUtils_.getInstance_(context);
        this.context = context;
        this.taskList = taskList;
        this.whichPage = whichPage;
    }

    @Override
    public MyOrderHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_task_list_item_rv, parent, false);
        return new MyOrderHolder(view, onItemClickListener);
    }

    @Override
    public void onBindViewHolder(MyOrderHolder holder, int position) {
        final int pos = holder.getLayoutPosition();
        TaskBean bean = taskList.get(pos);

        holder.tv_taskNo.setText(String.format(context.getString(R.string.string_taskList_orderNo), bean.getMissionCode()));

        holder.tv_fromAddress.setText(bean.getStartAddress());
        holder.tv_toAddress.setText(bean.getEndAddress());

        String goodsName = bean.getGoodsName();
        String weight = StringUtils.formatWeight(bean.getGoodsWeight());
        String volume = StringUtils.formatVolume(bean.getGoodsVolume());
        String goodsCount = bean.getGoodsCount();
        holder.tv_goodsName.setText(goodsName + "   " + weight + "/" + volume + "   " + goodsCount + "件");

        holder.tv_orderTime.setText(dateUtils.formatData(bean.getSubmitTime(), "yy-M-d HH:mm"));

        int needToPayMoney = NumberUtils.formatStringToInt(bean.getDealPrice()) - NumberUtils.formatStringToInt(bean.getActualMoney());
        if (Page_Current == whichPage // 从当前任务跳转过来
                && Value.TaskSource_Collect.equals(bean.getCollectGoods()) // 收货
                && needToPayMoney > 0 // 需付金额 >0
                && (Value.PayType_OnLine.equals(bean.getPayType()) || Value.PayType_Cash.equals(bean.getPayType()))) { // 在线支付/现金
            showPayMoneyView(true, holder);
            holder.tv_needToPay.setText("￥" + moneyUtil.format2Yuan(needToPayMoney));
        } else {
            showPayMoneyView(false, holder);
        }
    }

    /**
     * 显示需付款的View
     *
     * @param isShow
     * @param holder
     */
    private void showPayMoneyView(boolean isShow, MyOrderHolder holder) {
        int visibilityStatus = isShow ? View.VISIBLE : View.GONE;
        holder.iv_moneyStatus.setVisibility(visibilityStatus);
        holder.tv_needToPayLabel.setVisibility(visibilityStatus);
        holder.tv_needToPay.setVisibility(visibilityStatus);
    }

    @Override
    public int getItemCount() {
        return taskList != null ? taskList.size() : 0;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public static class MyOrderHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private OnItemClickListener onItemClickListener;

        private TextView tv_taskNo;
        private TextView tv_fromAddress;
        private TextView tv_toAddress;
        private TextView tv_goodsName;
        //        private TextView tv_WeightVolume;
//        private TextView tv_goodsCount;
        private ImageView iv_moneyStatus;
        private TextView tv_needToPayLabel;
        private TextView tv_needToPay;
        private TextView tv_orderTime;

        public MyOrderHolder(View itemView, OnItemClickListener onItemClickListener) {
            super(itemView);

            tv_taskNo = (TextView) itemView.findViewById(R.id.tv_taskNo);
            tv_fromAddress = (TextView) itemView.findViewById(R.id.tv_fromAddress);
            tv_toAddress = (TextView) itemView.findViewById(R.id.tv_toAddress);
            tv_goodsName = (TextView) itemView.findViewById(R.id.tv_goodsName);
//            tv_WeightVolume = (TextView) itemView.findViewById(R.id.tv_WeightVolume);
//            tv_goodsCount = (TextView) itemView.findViewById(R.id.tv_goodsCount);
            iv_moneyStatus = (ImageView) itemView.findViewById(R.id.iv_moneyStatus);
            tv_needToPayLabel = (TextView) itemView.findViewById(R.id.tv_needToPayLabel);
            tv_needToPay = (TextView) itemView.findViewById(R.id.tv_needToPay);
            tv_orderTime = (TextView) itemView.findViewById(R.id.tv_orderTime);

            this.onItemClickListener = onItemClickListener;
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(getPosition());
            }
        }
    }
}